<?php

namespace post\modules\post\models;

use post\modules\user\models\User;
use Yii;
use yii\behaviors\TimestampBehavior;


class Post extends \yii\db\ActiveRecord
{
    
    public static function tableName()
    {
        return '{{%post}}';
    }

   public function behaviors()  
    {  
        return [  
            [
//                TimestampBehavior
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    \yii\db\ActiveRecord::EVENT_BEFORE_INSERT => ['created_at','updated_at'],
                    \yii\db\ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at']
                ],  
            ],  
        ];  
    }


    public   function get_validate($user)
    {
//        $this->find()->where([''])->one()
        if($user->toArray()['id']!=$this->toArray()['user_id']){
            return "false";
        }else{
            return "true";
        }
    }


    public function  getZans()
    {
        $id=$this->toArray()['id'];
        return Zan::find()->where(['post_id'=>$id])->count();
    }


    public function  getComments()
    {
        $id=$this->toArray()['id'];
        return Comment::find()->where(['post_id'=>$id])->count();
    }

    public function  getUser()
    {
        return $this->hasOne(User::className(),['id'=>'user_id'])
            ->select(['id','username'])->asArray();
    }





}
